package com.example.nativesharelibrary;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import androidx.core.content.FileProvider;

import com.example.nativesharelibrary.callback.NoInstalled;
import com.example.nativesharelibrary.utils.FileOpen;

import java.io.File;

/**
 * 系统原生分享
 */
public class Share {
    /**
     * 分享的文字内容
     */
    private final String text;

    /**
     * 分享选择标题
     */
    private final String chooserTitle;

    /**
     * 分享类型
     */
    private final int shareType;

    /**
     * 分享的文件
     */
    private final File shareFile;

    /**
     * 监听事件
     */
    private final NoInstalled noInstalled;

    /**
     * 分享的平台类型
     */
    private final int sharePlatform;


    public String getText() {
        return text;
    }

    public String getChooserTitle() {
        return chooserTitle;
    }

    public int getShareType() {
        return shareType;
    }

    public File getShareFile() {
        return shareFile;
    }

    public NoInstalled getNoInstalled() {
        return noInstalled;
    }

    public int getSharePlatform() {
        return sharePlatform;
    }


    Share(ShareBuilder builder) {
        chooserTitle = builder.getChooserTitle();
        text = builder.text;
        shareType = builder.shareType;
        sharePlatform = builder.sharePlatform;
        shareFile = builder.shareFile;
        noInstalled = builder.noInstalled;
    }

    /**
     * 分享开始
     *
     * @param context 上下文
     */
    public void shareForSystem(Context context) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (shareType == ShareEm.SHARE_TEXT) {
            shareIntent.putExtra(Intent.EXTRA_TEXT, getText());
            shareIntent.setType("text/plain");
        } else if (shareType == ShareEm.SHARE_FILE) {
            if (getShareFile() == null) {
                showError("分享的文件为空");
                return;
            }
            Uri fileUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                fileUri = FileProvider.getUriForFile(context,
                        context.getPackageName() + ".wechatShare",
                        getShareFile());
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                fileUri = Uri.fromFile(getShareFile());
            }
            shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
            shareIntent.setType(FileOpen.getMimeTypeFromFile(getShareFile()));
        }
        if (getSharePlatform() == ShareEm.PLA_QQ) {
            shareIntent.setComponent(new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"));
        } else if (getSharePlatform() == ShareEm.PLA_WX) {
            shareIntent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"));
        } else if (getSharePlatform() == ShareEm.PLA_WX_MOMENT) {
            shareIntent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"));
        } else if (getSharePlatform() == ShareEm.PLA_ALIPAY) {
            shareIntent.setComponent(new ComponentName("com.eg.android.AlipayGphone", "com.alipay.mobile.quinox.splash.ShareDispenseActivity"));
        }
        context.startActivity(Intent.createChooser(shareIntent, getChooserTitle()));
    }

    private void showError(String error) {
        if (getNoInstalled() != null) {
            getNoInstalled().noInstalled(error);
        }
    }

    public static ShareBuilder builder() {
        return new ShareBuilder();
    }


    public static class ShareBuilder {

        /**
         * 分享的文字内容
         */
        public String text;

        /**
         * 分享选择标题
         */
        public String chooserTitle = "分享";

        /**
         * 分享类型
         */
        public int shareType;

        /**
         * 分享的文件
         */
        public File shareFile;

        /**
         * 监听事件
         */
        public NoInstalled noInstalled;

        /**
         * 分享的平台类型
         */
        public int sharePlatform = ShareEm.PLA_ALL;

        private ShareBuilder() {

        }

        public ShareBuilder setText(String text) {
            this.text = text;
            return this;
        }

        public String getChooserTitle() {
            return chooserTitle;
        }

        public ShareBuilder setChooserTitle(String chooserTitle) {
            this.chooserTitle = chooserTitle;
            return this;
        }

        public ShareBuilder setShareType(@ShareType int shareType) {
            this.shareType = shareType;
            return this;
        }


        public ShareBuilder setSharePlatform(@SharePlatform int sharePlatform) {
            this.sharePlatform = sharePlatform;
            return this;
        }

        public ShareBuilder setShareFile(File shareFile) {
            this.shareFile = shareFile;
            return this;
        }

        public ShareBuilder setNoInstalled(NoInstalled noInstalled) {
            this.noInstalled = noInstalled;
            return this;
        }

        public Share build() {
            return new Share(this);
        }
    }
}
